#include "logger.h"

#define RUTA_SALIDA	".\\Logger"

Logger* Logger::instance = NULL;

Logger* Logger::get_instance(){
	if (Logger::instance == NULL) 
			Logger::instance = new Logger();
	return instance;	
}

void Logger::destroy(){
	delete Logger::instance;
	Logger::instance = NULL;	
}

void Logger::comenzar_log(){
	terminar_log();
	std::string nombre = RUTA_SALIDA;
	nombre += "XMLParserResults.txt";
	this->arch_out.open(nombre.c_str());	
}
		
void Logger::terminar_log(){
	if( hay_log_abierto() )	
		arch_out.close();
}

bool Logger::isDebugMode(){
	return debugMode;
}

void Logger::log(std::string log ) throw (CloseLoggerExcep){
	if(isDebugMode()){
		if( hay_log_abierto() ){
			arch_out << "[INFO] "<< log << "\n";
			arch_out.flush();
			std::cout << "[INFO] "<< log <<"\n" << std::flush;
		}
		else
			throw CloseLoggerExcep("Error - Archivo de Log cerrado");
	}
}
void Logger::log(Figure* figure) throw (CloseLoggerExcep){
	if(isDebugMode()){
		if( hay_log_abierto() ){
			arch_out << "[INFO] "<< figure->getCreationLog() << "\n";
			arch_out.flush();
			std::cout << "[INFO] "<< figure->getCreationLog() <<"\n" << std::flush;
		}
		else
			throw CloseLoggerExcep("Error - Archivo de Log cerrado");
	}
};

void Logger::log(std::runtime_error exception) throw (CloseLoggerExcep){
	if( hay_log_abierto() ){
		arch_out << "[ERROR] " << exception.what() << "\n";
		arch_out.flush();
		std::cout << "[ERROR] " << exception.what()<< "\n" << std::flush;
	}
	else
		throw CloseLoggerExcep("Error - Archivo de Log cerrado");
}

bool Logger::hay_log_abierto(){
	return arch_out.is_open();
}

